<?php
namespace app\index\controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:POST,GET");

use app\common\controller\HomeBase;
use app\index\model\Secondhandle as Second;
use think\Db;

class Secondhandle1 extends HomeBase
{
	protected function _initialize(){
		parent::_initialize();
	}


	/**
	*二手车首页品牌显示
	**/
	public function brand()
	{
		//获取品牌数组
		$list = Db('used_classify')->select();
        $i = 0;
        for (; $i < count($list); $i++) {
        	$stack = explode(',',$list[$i]['pid']);
        	if(count($stack) > 1){
				$num1 = array_shift($stack);
        		$num2 = array_shift($stack);
        		if ($num2 == 0 && $num1 > 0) {
        			$arr[] = $list[$i]['name'];
        		}	
        	}        	
        }
        //去除品牌名字重复的
        $arr = array_unique($arr);
        $j = 0;
        for (; $j < count($arr); $j++) {
        	$name = $arr[$j];
        	$brands[$j] = Db('used_classify')->where('name',$name)->find();
        }
        return $brands;

	}

	/**
	*二手车搜索页面
	*@param   $method        排序方式id
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	*@param   $type 		 车的类型
	*@param   $brand  		 车的品牌
	*@param   $low  		 查询最低价
	*@param   $high      	 查询最高价
	*@param   $arr      	 地址数组(包含省份id,地级市id)
	*@param   $title         搜索标题
	**/
	public function showCar()
	{

		$method = (isset($_GET['method']) ? $_GET['method']:0);
		//分页设置
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:10);
		$low = (isset($_GET['low']) ? $_GET['low']:0);
		$high =(isset($_GET['high']) ? $_GET['high']:10000);
		$type = (isset($_GET['type']) ? $_GET['type']:0);
		$brand =(isset($_GET['brand']) ? $_GET['brand']:0);
		$arr = (isset($_GET['arr']) ? $_GET['arr']:'');
		$title = (isset($_GET['title']) ? $_GET['title']:'');

		if($method == 0){
			//默认
			$list = $this->doShow('id','desc',$page,$pagelimit,$type,$brand,$low,$high,$arr,$title);
			$this->dohtml($list,$page,$pagelimit);
		}elseif($method == 1){
			//最新
			$list = $this->doShow('create_time','desc',$page,$pagelimit,$type,$brand,$low,$high,$arr,$title);
			$this->dohtml($list,$page,$pagelimit);
		}elseif($method ==2){
			//价格最低
			$list = $this->doShow('price','esc',$page,$pagelimit,$type,$brand,$low,$high,$arr,$title);
			$this->dohtml($list,$page,$pagelimit);
		}elseif($method ==3){
			//价格最高
			$list = $this->doShow('price','desc',$page,$pagelimit,$type,$brand,$low,$high,$arr,$title);
			$this->dohtml($list,$page,$pagelimit);
		}elseif($method ==4){
			//上牌时间倒序
			$list = $this->doShow('apply_time','desc',$page,$pagelimit,$type,$brand,$low,$high,$arr,$title);
			$this->dohtml($list,$page,$pagelimit);
		}elseif($method ==5){
			//降价急售
			$list = $this->doIfShow('low_price',1,$page,$pagelimit,$type,$brand,$low,$high,$arr,$title);
			$this->dohtml($list,$page,$pagelimit);
		}elseif($method ==6){
			//好车推荐
			$list = $this->doIfShow('is_recommend',1,$page,$pagelimit,$type,$brand,$low,$high,$arr,$title);
			$this->dohtml($list,$page,$pagelimit);
		}else{
			$this->apiError('没有查到信息');
		}

	}

	/**
	*公用方法:order 排序
	*@param   $field 		 按照字段段排序
	*@param   $way  		 排序方法
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	*@param   $type 		 车的类型
	*@param   $brand  		 车的品牌
	*@param   $low  		 查询最低价
	*@param   $high      	 查询最高价
	*@param   $arr      	 地址数组(包含省份id,地级市id)
	*@param   $title         搜索标题
	**/
	public function doShow($field,$way,$page,$pagelimit,$type,$brand,$low,$high,$arr,$title)
	{
		if($title){
            $map['title'] = ['like',"%{$title}%"];
        }
		if($arr){
			$map['site'] = ['like',"%{$arr}%"];
		}
        if($type){
            $map['type'] = $type;
        }
        if($brand){
            $map['brand'] = $brand;
        }       
        $map['status'] = 1;
        $map['deal'] = 0;

        $list1 = Db('used_truck')->where('is_top',1)->where('price','>=',$low)->where('price','<',$high)->where($map)->order("{$field}","{$way}")->page($page,$pagelimit)->select();
			
			//二手车的状态 审核,不指顶,未交易
		$list2 = Db('used_truck')->where('is_top',0)->where('price','>=',$low)->where('price','<',$high)->where($map)->order("{$field}","{$way}")->page($page,$pagelimit)->select();
		$list = array_merge($list1,$list2);
		return $list;		
	}


	/**
	*公用方法:where 判断
	*@param   $field 		 按照字段段排序
	*@param   $way  		 排序方法
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	*@param   $type 		 车的类型
	*@param   $brand  		 车的品牌
	*@param   $low  		 查询最低价
	*@param   $high      	 查询最高价
	**/
	public function doIfShow($field,$value,$page,$pagelimit,$type,$brand,$low,$high,$arr,$title)
	{
		if($title){
            $map['title'] = ['like',"%{$title}%"];
        }
		if($arr){
			$map['site'] = implode(',',$arr);
		}
        if($type){
            $map['type'] = $type;
        }
        if($brand){
            $map['brand'] = $brand;
        }       
        $map['status'] = 1;
        $map['deal'] = 0;
        //查询
        $list1 = Db('used_truck')->where('is_top',1)->where($map)->where('price','>=',$low)->where('price','<',$high)->where("{$field}","{$value}")->order("create_time","desc")->page($page,$pagelimit)->select();
		$list2 = Db('used_truck')->where('is_top',0)->where($map)->where('price','>=',$low)->where('price','<',$high)->where("{$field}","{$value}")->order("create_time","desc")->page($page,$pagelimit)->select();
		$list = array_merge($list1,$list2);
		return $list;
	}

	/**
	*公共方法:将数组中的图集,车辆所在地,标题,表显里程,全款价格,上牌时间,交强险过期时间,卖家附言进行处理
	*@param   $arr 		 	 处理的数组
	*@param   $page  		 第几页
	*@param   $pagelimit 	 每页几个
	**/
	public function dohtml($arr,$page,$pagelimit)
	{
		//dump($arr1);
		$num = count($arr);
		if($num){
			$i = 0;
			for(;$i<count($arr);$i++){
				$arr[$i]['photo'] = unserialize($arr[$i]['photo']);
				$arr1[] = $arr[$i];
			}
			$count = count($arr1);
			$pagecount = ceil($count/$pagelimit);
			// $List['pagecount'] = $pagecount;
			$list['group'] = $arr1;
			$list['pagecount'] = $pagecount;
			// dump($list);
			 $this->apiSuccess($list);
		}else{
			 $this->apiError('没有查到信息');
		}
		
	}

	/**
    *轮播图
    **/
    public function showPhoto($id=1)
    {
        $showPhoto_list = Db('slide')->where('cid',3)->select();
        return $showPhoto_list;
    }


	/**
	*首页显示
	**/
	public function firstShow()
	{
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:10);
		$low = (isset($_GET['low']) ? $_GET['low']:0);
		$high =(isset($_GET['high']) ? $_GET['high']:10000);
		$type = (isset($_GET['type']) ? $_GET['type']:0);
		$brand =(isset($_GET['brand']) ? $_GET['brand']:0);
		$arr = (isset($_GET['arr']) ? $_GET['arr']:'');
		$title = (isset($_GET['title']) ? $_GET['title']:'');
		//轮播图
		$list['lunbo'] = $this->showPhoto();
		//首页品牌
		$list['brand'] = $this->brand();

		//这两个
		//最新
		$latest = $this->doShow('create_time','desc',$page,$pagelimit,$type,$brand,$low,$high,$arr,$title);

		$i = 0;
		for(;$i<count($latest);$i++){
			$latest[$i]['photo'] = unserialize($latest[$i]['photo']);
			$arr1[] = $latest[$i];
		}
		$list['latest'] = $arr1;
		//推荐
		$recommend = $this->doIfShow('is_recommend',1,$page,$pagelimit,$type,$brand,$low,$high,$arr,$title);

		$i = 0;
		for(;$i<count($recommend);$i++){
			$recommend[$i]['photo'] = unserialize($recommend[$i]['photo']);
			$arr2[] = $recommend[$i];
		}
		$list['recommend'] = $arr2;
		// dump($list);
		$this->apiSuccess($list);
	}

}